package cn.edu.scau.cmi.ema.domain;


import com.alibaba.fastjson.annotation.JSONField;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllDetectstandards", query = "select myDetectstandard from Detectstandard myDetectstandard"),
		@NamedQuery(name = "findDetectstandardByName", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.name = ?1"),
		@NamedQuery(name = "findDetectstandardByNameContaining", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.name like concat('%',?1,'%')"),
		@NamedQuery(name = "findDetectstandardByDate", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.date = ?1"),
		@NamedQuery(name = "findDetectstandardByMin", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.min = ?1"),
		@NamedQuery(name = "findDetectstandardByMax", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.max = ?1"),
		@NamedQuery(name = "findDetectstandardByDescription", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.description = ?1"),
		@NamedQuery(name = "findDetectstandardByDescriptionContaining", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.description like concat('%',?1,'%')"),
		@NamedQuery(name = "findAllDetectstandardsByFeature", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.feature.id = ?1"),
		@NamedQuery(name = "findAllDetectstandardsByFood", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.food.id = ?1"),
		@NamedQuery(name = "findAllDetectstandardsByUnit", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.unit.id = ?1"),
		@NamedQuery(name = "findDetectstandardById", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.id = ?1"),
		@NamedQuery(name = "findDetectstandardByPrimaryKey", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.id = ?1 "),
		@NamedQuery(name = "deleteDetectstandardsByIds", query = "delete Detectstandard where id in (?1)"),
		@NamedQuery(name = "findDetectstandardsByIds", query = "select myDetectstandard from Detectstandard myDetectstandard where myDetectstandard.id in (?1)"),
		@NamedQuery(name = "findDetectstandardByPhyscialPrimaryKey", query = "select myDetectstandard from Detectstandard myDetectstandard where  myDetectstandard.food.id = ?1 and myDetectstandard.feature.id = ?2 and myDetectstandard.date = ?3"),
		@NamedQuery(name = "countDetectstandardsByName", query = "select count(myDetectstandard)  from Detectstandard myDetectstandard where myDetectstandard.name like concat('%',?1,'%')") })
@Table(catalog = "ema", name = "detectstandard")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "ema/cn/edu/scau/cmi/ema/domain", name = "Detectstandard")
@XmlRootElement(namespace = "ema/cn/edu/scau/cmi/ema/domain")

public class Detectstandard implements Serializable, Comparable<Detectstandard> {

	private static final long serialVersionUID = 1L;

	@Column(name = "id", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@XmlElement
	Integer id;

	@Column(name = "name")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String name;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "date")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Calendar date;

	@Column(name = "min", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal min;

	@Column(name = "max", precision = 22)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	BigDecimal max;

	@Column(name = "description")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String description;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "feature", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Feature feature;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "food", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Food food;
	@JSONField(serialize = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumns({ @JoinColumn(name = "unit", referencedColumnName = "id", nullable = false) })
	@XmlTransient
	Unit unit;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return this.id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return this.name;
	}

	public void setDate(Calendar date) {
		this.date = date;
	}

	public Calendar getDate() {
		return this.date;
	}

	public void setMin(BigDecimal min) {
		this.min = min;
	}

	public BigDecimal getMin() {
		return this.min;
	}

	public void setMax(BigDecimal max) {
		this.max = max;
	}

	public BigDecimal getMax() {
		return this.max;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return this.description;
	}

	public void setFeature(Feature feature) {
		this.feature = feature;
	}

	public Feature getFeature() {
		return feature;
	}

	public void setFood(Food food) {
		this.food = food;
	}

	public Food getFood() {
		return food;
	}

	public void setUnit(Unit unit) {
		this.unit = unit;
	}

	public Unit getUnit() {
		return unit;
	}

	public void copy(Detectstandard that) {

		setId(that.getId());

		setName(that.getName());
		setDate(that.getDate());
		setMin(that.getMin());
		setMax(that.getMax());
		setDescription(that.getDescription());

		setFeature(that.getFeature());

		setFood(that.getFood());

		setUnit(that.getUnit());

	}

	public Detectstandard() {

	}

	@Override
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");

		buffer.append("name=[").append(name).append("] ");
		buffer.append("date=[").append(date).append("] ");
		buffer.append("min=[").append(min).append("] ");
		buffer.append("max=[").append(max).append("] ");
		buffer.append("description=[").append(description).append("] ");

		return buffer.toString();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Detectstandard))
			return false;
		Detectstandard comparedDetectstandard = (Detectstandard) obj;
		if (!CheckPropertyEqual(this.food.id, comparedDetectstandard.food.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.feature.id, comparedDetectstandard.feature.id)) {
			return false;
		}
		if (!CheckPropertyEqual(this.date, comparedDetectstandard.date)) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}

	@Override
	public int compareTo(Detectstandard o) {
		if (this.getId() > o.getId()) {
			return 1;
		} else if (this.getId() == o.getId()) {
			return 0;
		} else {
			return -1;
		}

	}
}
